public class Solution129 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int ans=0;

    public int sumNumbers(TreeNode root) {
        dfs(root,0);
        return ans;
    }

    void dfs(TreeNode root,int cur){
        if (root==null){
            return;
        }
        cur=cur*10+root.val;
        if (root.left==null && root.right==null){
            ans=ans+cur;
        }else {
            dfs(root.left, cur);
            dfs(root.right, cur);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution129().sumNumbers(new TreeNode(1,new TreeNode(2),new TreeNode(3))));
    }
}
